package com.diandian.backend.kuaishou.life.exchange.controller;

import com.diandian.backend.kuaishou.life.exchange.service.BusinessCertificateVerifyService;
import com.diandian.backend.kuaishou.life.exchange.service.BusinessTokenGetService;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.kuaishou.life.boot.starter.param.CertificateCancelParam;
import com.diandian.kuaishou.life.boot.starter.param.CertificatePrepareParam;
import com.diandian.kuaishou.life.boot.starter.param.CertificateVerifyParam;
import com.diandian.kuaishou.life.boot.starter.service.KuaiShouCertificateBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/certificate")
public class CertificateController {

    @Autowired
    private KuaiShouCertificateBasicService certificateBasicService;

    @Autowired
    private BusinessCertificateVerifyService businessCertificateVerifyService;

    @Autowired
    private BusinessTokenGetService businessTokenGetService;

    /**
     * 验券准备
     */
    @PostMapping( "/prepare")
    public BaseResult prepare(@RequestHeader(name = "appKey") String appKey,
                              @RequestHeader(name = "appSecret") String appSecret,
                              @RequestHeader(value = "merchantId") String merchantId,
                              @RequestBody CertificatePrepareParam param) {
        String accessToken = businessTokenGetService.getToken(appKey, appSecret, merchantId);
        return new BaseResult(0, "success", certificateBasicService.prepare(accessToken, param));
    }

    /**
     * 验券准备（带订单详情）
     */
    @PostMapping( "/prepareWithOrderDetail")
    public BaseResult prepareWithOrderDetail(@RequestHeader(name = "appKey") String appKey,
                              @RequestHeader(name = "appSecret") String appSecret,
                              @RequestHeader(value = "merchantId") String merchantId,
                              @RequestBody CertificatePrepareParam param) {
        String accessToken = businessTokenGetService.getToken(appKey, appSecret, merchantId);
        return new BaseResult(0, "success", businessCertificateVerifyService.prepareWithOrderDetail(accessToken, param));
    }

    /**
     * 验券/核销接口
     */
    @PostMapping( "/verify")
    public BaseResult verify(@RequestHeader(name = "appKey") String appKey,
                             @RequestHeader(name = "appSecret") String appSecret,
                             @RequestHeader(value = "merchantId") String merchantId,
                             @RequestBody CertificateVerifyParam param) {
        String accessToken = businessTokenGetService.getToken(appKey, appSecret, merchantId);
        return new BaseResult(0, "success", certificateBasicService.verify(accessToken, param));
    }

    /**
     * 撤销核销
     */
    @PostMapping( "/cancel")
    public BaseResult cancel(@RequestHeader(name = "appKey") String appKey,
                             @RequestHeader(name = "appSecret") String appSecret,
                             @RequestHeader(value = "merchantId") String merchantId,
                             @RequestBody CertificateCancelParam param) {
        String accessToken = businessTokenGetService.getToken(appKey, appSecret, merchantId);
        return new BaseResult(0, "success", certificateBasicService.cancel(accessToken, param));
    }


}
